package com.example.lastwork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class login extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "login2";
    private EditText mEtLoginactivityUsername;
    private EditText mEtLoginactivityPassword;
    private Button mBtLoginactivityLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        initView();
        UserDao userDao = new UserDao();


        Log.i(TAG, "登录页面");

    }

    private void initView() {
        // 初始化控件
        mBtLoginactivityLogin = findViewById(R.id.button);
        mEtLoginactivityUsername = findViewById(R.id.E1);
        mEtLoginactivityPassword = findViewById(R.id.E2);

        // 设置点击事件监听器
        mBtLoginactivityLogin.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                String name = mEtLoginactivityUsername.getText().toString().trim();
                String pwd = mEtLoginactivityPassword.getText().toString().trim();
                String what = "password";
                String a = null;
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
                    UserDao userDao = new UserDao();
                    try {
                        a = UserDao.select(name,what);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        if (!TextUtils.isEmpty(a)) {
                            Log.i(TAG, "name=" + a);
                            try {
                                if (pwd.equals(a)) {
                                    Log.i(TAG, "password=" + a);
                                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    intent.setClass(login.this, MainActivity2.class);
                                    intent.putExtra("id",name);
                                    SharedPreferences sp = this.getSharedPreferences("rate", MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("id", name);
                                    String time = sp.getString("time",null);
                                    Log.i(TAG,"时间差"+time);
                                    editor.apply();
                                    startActivity(intent);
                                    finish();
                                    break;
                                } else {
                                    Toast.makeText(this, "密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(this, "用户名不存在", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }
                break;


        }
    }
}